package com.symaster.common.test.pid;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * @author yinmiao
 * @version 2023/11/25
 */
public class Util {
    public static float time() {
        return System.nanoTime() / 1000000f;
    }

    public static JComponent builderTextField(String labelText, String defaultText, KeyReleased keyReleased) {

        JLabel label = new JLabel(labelText);

        JTextField textField = new JTextField();
        textField.setText(defaultText);
        textField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                keyReleased.keyReleased(e, textField);
            }
        });
        textField.setPreferredSize(new Dimension(200, 25));

        JPanel jPanel = new JPanel();
        jPanel.setLayout(new BorderLayout());
        jPanel.add(label, BorderLayout.WEST);
        jPanel.add(textField, BorderLayout.CENTER);

        return jPanel;
    }

    public interface KeyReleased {
        void keyReleased(KeyEvent keyEvent, JTextField textField);
    }
}
